//
//  UIDevice+Ext.swift
//  ACFrameworkSwift
//
//  Created by fisherman on 2021/11/19.
//

import Foundation

extension UIDevice {
    
    /// iphone 刘海屏存在安全区域
    public static func isExistSafeArea() -> Bool {
        if #available(iOS 11.0, *) {
            return UIApplication.shared.keyWindow?.safeAreaInsets != nil
        }
        return false
    }
    
    /// 获取设备方向改变时的安全 frame
    /// - Parameter currentView: 当前所在视图，当存在 navbar、tabbar 时，安全边距会有所不同，不传入则默认用 window
    /// - Returns: CGRect 安全布局 frame
    public static func safeAreaFrameForDeviceOrientationChanged(currentView: UIView? = nil) -> CGRect {
        var safeEdges = UIEdgeInsets.zero
        if #available(iOS 11.0, *) {
            if let `currentView` = currentView {
                safeEdges = currentView.safeAreaInsets
            }else {
                safeEdges = UIApplication.shared.keyWindow?.safeAreaInsets ?? .zero
            }
        }
        // UIScreen.main.bounds 在设备旋转过程中，能获取实际的宽高，无需根据方向重新计算
        let screenFrame = UIScreen.main.bounds
        let safeFrame = screenFrame.inset(by: safeEdges)
        return safeFrame
    }
    
}
